package com.service;

import com.mapper.ConsumerMapper;
import com.pojo.Consumer;
import com.pojo.requestjson.FaceRecognitionRequest;
import com.service.iter.IConsumerService;
import com.service.iter.IFaceServiceImpl;
import com.util.JsonParser;
import com.util.Res;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消费者服务实施
 *
 * @author 蔡文欣
 * @date 2023/07/05 16:30:42
 */
@Service
@Transactional
public class ConsumerServiceImpl implements IConsumerService {
    @Autowired
    private IFaceServiceImpl faceService;
    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 添加消费者脸到人脸库
     *
     * @param faceRecognitionRequest 人脸识别请求
     * @return {@link Res}
     * @throws Exception 异常
     */
    @Override
    public Res addConsumerFace(FaceRecognitionRequest faceRecognitionRequest) throws Exception {

        Res res = new Res();

        /*
          s 是检测的响应json字符串
         faces 是获取s中的faces对象
         jsonArray 是把faces转为json格式
         */
        String s = faceService.detectFace(faceRecognitionRequest.getImageData());
        System.out.println(s);
        Object faceToken = null;
        Object faces = JsonParser.extractFaceToken(s, "faces");
        System.out.println(faces);
        assert faces != null;
        if (faces.toString().equals("[]")) {
            res.setMsg("添加失败");
            res.setCode(1);
            return res;
        }
        JSONArray jsonArray = new JSONArray(faces.toString());
        faces = jsonArray.get(0);
        System.out.println(faces);
        //以防空指针异常导致程序崩溃
        if (faces.toString().equals("")) {
            res.setMsg("添加失败");
            res.setCode(1);
            return res;
        } else {
            faceToken = JsonParser.extractFaceToken(faces.toString(), "face_token");
            try {
                assert faceToken != null;
                faceRecognitionRequest.setFaceToken(faceToken.toString());
                System.out.println(faceRecognitionRequest);
                faceService.addConsumerFace(faceRecognitionRequest);
                res.setMsg("添加成功");
                res.setCode(0);
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                res.setMsg("添加失败");
                res.setCode(1);
                return res;
            }
        }
    }


    /**
     * 查询面
     *
     * @param faceRecognitionRequest 人脸识别请求
     * @return {@link Res}
     */
    @Override
    public Res queryFace(FaceRecognitionRequest faceRecognitionRequest) throws Exception {
        Res res = new Res();
        System.out.println(faceRecognitionRequest);
        Consumer consumer = consumerMapper.byIdSelect(faceRecognitionRequest.getPhone());
        String consumerFaceToken = consumer.getConsumerFacetoken();
        String s = faceService.detectFace(faceRecognitionRequest.getImageData());
        System.out.println(s);
        Object faceToken = null;
        Object faces = JsonParser.extractFaceToken(s, "faces");
        System.out.println(faces);
        assert faces != null;


        if (faces.toString().equals("[]")) {
            res.setMsg("未检测到人脸");
            res.setCode(1);
            return res;
        }
        JSONArray jsonArray = new JSONArray(faces.toString());
        faces = jsonArray.get(0);
        System.out.println(faces);
        if (faces.toString().equals("")) {
            res.setMsg("未检测到人脸");
            res.setCode(1);
            return res;
        } else {
            faceToken = JsonParser.extractFaceToken(faces.toString(), "face_token");
            try {
                assert faceToken != null;
                boolean faceId = faceService.searchFace(faceToken.toString(), consumerFaceToken);
                System.out.println(faceId);
                faceRecognitionRequest.setFaceToken(faceToken.toString());
                System.out.println(faceRecognitionRequest);
                res.setMsg("人脸通过");
                res.setCode(0);
                Consumer query = consumerMapper.query(faceRecognitionRequest.getPhone(),faceRecognitionRequest.getFaceToken());
                System.out.println(faceRecognitionRequest.getFaceToken());
                System.out.println(query);
                if (query.getConsumerPassword()!=null) {
                    consumerMapper.updatePassword(faceRecognitionRequest.getPhone());
                }
                String consumerPassword = query.getConsumerPassword();
                return res;
            } catch (Exception e) {
                e.printStackTrace();
                res.setMsg("人脸不匹配");
                res.setCode(1);
                return res;
            }
        }
    }
}
